import Vue from 'vue'
import App from './App.vue'
import Vuex from 'vuex'
import {ADD,SUB} from '@/store/store.js'
import { getProducts } from "./api/shop";
let _product = getProducts()
Vue.config.productionTip = false
Vue.use(Vuex)


const moduleA = {
  namespaced: true,
  state:{
    products: _product,
  },
  mutations: { },
  actions: {  },
  getters: { }
}

const moduleB = {
  namespaced:true,
  state:{
      carts:[]
  },
  mutations: {
      addToCarts(state,data){
        let index=state.carts.findIndex((item,index)=>{
          if(item.id===data.id){
            return true
          }
        })
        if(index!=-1){
          state.carts[index].num++
        }else{
          let cart=JSON.parse(JSON.stringify(data))
          cart.num=1
          state.carts.push(data)
        }
      }
   },
  actions: {

  }
}



let store=new Vuex.Store({
  strict:true,
  modules: {
    a: moduleA,
    b: moduleB
  },
  state:{
    counter:666,
    speed:'奇数',
  },
  actions:{
    AsyncAdd(store,data){
      setTimeout(()=>{
        store.commit(ADD,data)
      },1000)
    }
  },
  mutations:{
    [ADD](state,data){
      state.counter+=data;
    },
    [SUB](state,data){
      state.counter-=data.data;
    },
  },
  getters:{
    isShow(state){
        return state.counter%2===0? "偶数" : '奇数'
    }
  }
})




new Vue({
  store,
  render: h => h(App),
}).$mount('#app')
